package com.jf.viewadapter;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.jf.assistant.JfLog;
import com.jf.assistant.SettingActivity;
import com.jf.assistant.databinding.LyActiontasklistBinding;
import com.jf.bean.AssistantTask;
import com.jf.controller.MainController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

// ① 创建Adapter
public  class ActionTaskListAdapter extends RecyclerView.Adapter<ActionTaskListAdapter.VH>{
    //② 创建ViewHolder
    public static class VH extends RecyclerView.ViewHolder{
        public final TextView actionname;
        public final TextView actiontime;
        public final Button actionedit;
        public  Button actiondele;
        public VH(LyActiontasklistBinding v) {
            super(v.getRoot());
            actionname = v.tvActionname;
            actiontime=v.tvActiontime;
            actionedit=v.btEdit;
            actiondele=v.btTaskdele;
        }
    }

    private ArrayList<AssistantTask> mDatas;
    private  Activity mActivity;
    public ActionTaskListAdapter(ArrayList<AssistantTask> data, Activity mactivity) {
        this.mDatas = data;
        this.mActivity=mactivity;
    }

    //③ 在Adapter中实现3个方法

    @Override
    public void onBindViewHolder(ActionTaskListAdapter.VH holder, final int position) {
        holder.actionname.setText(mDatas.get(position).taskname);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:MM:SS");
       // Date date=mDatas.get(position).getStarttime();
         String format = mDatas.get(position).getStarttime().getTime().getHours()+":"
        +mDatas.get(position).getStarttime().getTime().getMinutes();
         holder.actiontime.setText(format);
        holder.actionedit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mActivity, SettingActivity.class);

                Bundle bundle = new Bundle();
                bundle.putSerializable("actions", mDatas.get(position).getActions());
                bundle.putSerializable("position", position);
                JfLog.logger( "chuan de data :"+mDatas.get(position).getActions().size());
                intent.putExtras(bundle);
               // intent.putExtra("action",mDatas.get(position).getActions());
                mActivity.startActivity(intent);
            }
        });
        holder.actiondele.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDatas.remove(position);
                try {
                    MainController.deleAssistantTask(position,mActivity);
                    notifyDataSetChanged();
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    @NonNull
    @Override
    public ActionTaskListAdapter.VH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //LayoutInflater.from(parent.getContext()).inflate(R.layout.item_rv, parent, false);
        LyActiontasklistBinding inflate = LyActiontasklistBinding.inflate(LayoutInflater.from(mActivity),parent,false);
        VH vh = new VH(inflate);
        return vh;
    }
}